import {
	_decorator,
	Component,
	Node,
	AudioSource,
	AudioSourceComponent,
	error,
	AudioClip,
} from "cc";
const { ccclass, property } = _decorator;

@ccclass("GameManager")
export class GameManager extends Component {
	private _volume: number = 1.0;

	public instance: GameManager = null;

	public set volume(val: number) {
		if (val > 1.0) {
			val = 1.0;
		}
		this.volume = val;
	}
    
    public get volume(){
        return this.volume;
    }

	@property(AudioSourceComponent)
	audioSource: AudioSourceComponent = null!;

	onLoad() {
		if (!this.audioSource) {
			this.audioSource = this.node.getComponent(AudioSourceComponent);
			if (!this.audioSource) {
				error("没有没有默认音频组件");
			}
		}
	}
	start() {
		// [3]
	}

	// update (deltaTime: number) {
	//     // [4]
	// }
	playOneShot(clip: AudioClip, volume: number = 1.0) {
		this.audioSource.playOneShot(clip, volume);
	}

	playMusic(clip: AudioClip, loop: boolean = false) {
        this.audioSource.clip=clip;
		this.audioSource.loop = loop;
		this.audioSource.volume = this._volume;
        this.audioSource.play();
	}
}
